package com.cn.pg.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
// 数据源配置类
public class MyRoutingDataSource {
    Logger logger = LoggerFactory.getLogger(MyRoutingDataSource.class);


    @Bean
    @ConfigurationProperties(prefix = "spring.druid.datasource.master")
    public DataSource masterDataSource(){
        logger.info("create master dataSource...");
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.druid.datasource.slave")
    public DataSource slaveDataSource(){
        logger.info("create master slave...");
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DataSource primaryDataSource(
            @Autowired @Qualifier("masterDataSource") DataSource masterDataSource,
            @Autowired @Qualifier("slaveDataSource") DataSource slaveDataSource
    ){
        Map<Object, Object> map = new HashMap<>();
        map.put("master", masterDataSource);
        map.put("slave", slaveDataSource);

        RoutingDataSource routingDataSource = new RoutingDataSource();
        routingDataSource.setTargetDataSources(map);
        return  routingDataSource;
    }
}
